package com.young4j.springdemo.controller;

import com.young4j.springdemo.config.event.BlackEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.stream.IntStream;

/**
 * @Description
 *
 * @Author yf
 * @Date 2018/12/26 17:00
 **/
@RestController
@RequestMapping("/event")
public class EventController {
    @Autowired
    private ApplicationContext applicationContext;
    @RequestMapping("/black")
    public Object black(){
        IntStream.range(0,1000).forEach(n ->{
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    BlackEvent event = new BlackEvent(this, Integer.toString(n));
                    applicationContext.publishEvent(event);
                    System.out.println("发布事件" + n + "成功！");
                }
            };
            Thread t = new Thread(runnable);
            t.run();
        });
        BlackEvent event = new BlackEvent(this, "black_event");
        applicationContext.publishEvent(event);
        System.out.println("ok！");
        return "success";
    }
}
